import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { AtToast } from 'taro-ui'
import { post } from '../../service/api'

import './index.scss'



class Dialog extends Component {
  constructor() {
    super(...arguments)
    this.state = {
      isShowDialog: false,
      auth: false,
      isShowToast: false,
      msg: "",
      login: false
    }
  }
  handleDialog() {
    this.props.onHandleDialog()
  }
  pop(msg) {
    this.setState({
      isShowToast: true,
      msg: msg
    }, () => {
      setTimeout(() => {
        this.setState({
          isShowToast: false
        })
      }, 1500)
    })
  }
  async toPagePhone() {
    const res = await Taro.login();
    const res2 = await Taro.getUserInfo()
    const { data } = await post('login', {
      jscode: res.code,
      encryptedData: res2.encryptedData,
      iv: res2.iv
    })
    if (data.code == 0) {
      Taro.setStorageSync('sessionKey', data.sessionKey);
      if (data.loginStatus && data.loginStatus == 'ok') {
        this.pop("登录成功")
        Taro.switchTab({ url: '/pages/index/index' });
        this.handleDialog()
        return;
      }
      if (this.state.auth) {
        Taro.navigateTo({ url: '/pages/phoneLogin/index' });
      }
    }
    this.handleDialog()
  }
  async toPagePass() {
    const res = await Taro.login();
    const res2 = await Taro.getUserInfo()
    const { data } = await post('login', {
      jscode: res.code,
      encryptedData: res2.encryptedData,
      iv: res2.iv
    })
    if (data.code == 0) {
      Taro.setStorageSync('sessionKey', data.sessionKey);
      if (data.loginStatus && data.loginStatus == 'ok') {
        this.pop("登录成功")
        Taro.switchTab({ url: '/pages/index/index' });
        this.handleDialog()
        return;
      }
      if (this.state.auth) {
        Taro.navigateTo({ url: '/pages/passLogin/index' });
      }
    }
    this.handleDialog()
  }
  bindGetUserInfo(e) {
    if (e.detail.userInfo) {

      this.setState({
        auth: true
      })
    }
  }
  render() {
    const { isShowToast, msg, } = this.state
    return (
      <View className="dialog-box">
        <View className="d-mask" onClick={this.handleDialog.bind(this)}></View>
        <View className="dialog">
          <View className="dialog-title">{this.props.title} <Image onClick={this.handleDialog.bind(this)} className="img" src={require('../../asset/imgs/Shape.png')} /></View>
          <View className="btn-wrapper">
            <View className="phone-btn"
              onClick={this.toPagePhone.bind(this)}>
              <Image className="img" src={require('../../asset/imgs/phone.png')} />
              <Button open-type="getUserInfo" className="btn" onGetUserInfo={this.bindGetUserInfo.bind(this)}></Button>
              手机登录
            </View>
            <View className="wx-btn"
              onClick={this.toPagePass.bind(this)}>
              <Image className="img" src={require('../../asset/imgs/user.png')} />
              <Button open-type="getUserInfo" className="btn" onGetUserInfo={this.bindGetUserInfo.bind(this)}></Button>
              账号密码登录
            </View>
          </View>
        </View>
        <AtToast
          isOpened={isShowToast}
          text={msg}
          duration={2000}
        ></AtToast>
      </View>
    )
  }
}

export default Dialog
